package com.dkd.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.dto.ChannelDto;
import com.dkd.manage.domain.vo.ChannelVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.service.IChannelService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;

/**
 * 售货机货道相关接口
 * 
 * @author fuyuncheng
 * @date 2025-05-06
 */
@Api(value = "/manage/channel", tags = {"售货机货道相关接口"})
@RestController
@RequestMapping("/manage/channel")
public class ChannelController extends BaseController
{
    @Autowired
    private IChannelService channelService;

    /**
     * 查询售货机货道列表
     */
    @ApiOperation(value = "查询售货机货道列表", notes = "查询售货机货道列表", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('manage:channel:list')")
    @GetMapping("/list")
    public TableDataInfo list(Channel channel)
    {
        startPage();
        List<Channel> list = channelService.selectChannelList(channel);
        return getDataTable(list);
    }

    /**
     * 导出售货机货道列表
     */
    @ApiOperation(value = "导出售货机货道列表", notes = "导出售货机货道列表", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('manage:channel:export')")
    @Log(title = "售货机货道", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Channel channel)
    {
        List<Channel> list = channelService.selectChannelList(channel);
        ExcelUtil<Channel> util = new ExcelUtil<Channel>(Channel.class);
        util.exportExcel(response, list, "售货机货道数据");
    }

    /**
     * 获取售货机货道详细信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "获取售货机货道详细信息", notes = "获取售货机货道详细信息", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('manage:channel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(channelService.selectChannelById(id));
    }

    /**
     * 新增售货机货道
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Channel", name = "channel", value = "", required = true)
    })
    @ApiOperation(value = "新增售货机货道", notes = "新增售货机货道", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('manage:channel:add')")
    @Log(title = "售货机货道", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Channel channel)
    {
        return toAjax(channelService.insertChannel(channel));
    }

    /**
     * 修改售货机货道
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Channel", name = "channel", value = "", required = true)
    })
    @ApiOperation(value = "修改售货机货道", notes = "修改售货机货道", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('manage:channel:edit')")
    @Log(title = "售货机货道", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Channel channel)
    {
        return toAjax(channelService.updateChannel(channel));
    }

    /**
     * 删除售货机货道
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "Long[]", name = "ids", value = "", required = true)
    })
    @ApiOperation(value = "删除售货机货道", notes = "删除售货机货道", httpMethod = "DELETE")
    @PreAuthorize("@ss.hasPermi('manage:channel:remove')")
    @Log(title = "售货机货道", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(channelService.deleteChannelByIds(ids));
    }

    /**
     * 根据售货机编号查询货道列表
     * @param innerCode
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "innerCode", value = "", required = true)
    })
    @ApiOperation(value = "根据售货机编号查询货道列表", notes = "根据售货机编号查询货道列表", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('manage:channel:list')")
    @GetMapping("/list/{innerCode}")
    public AjaxResult listByInnerCode(@PathVariable("innerCode") String innerCode)
    {
        List<ChannelVo> channelVoList = channelService.selectChannelVoListByInnerCode(innerCode);
        return AjaxResult.success(channelVoList);
    }

    /**
     * 修改商品货道关联
     * @param channelDto
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ChannelDto", name = "channelDto", value = "", required = true)
    })
    @ApiOperation(value = "修改商品货道关联", notes = "修改商品货道关联", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('manage:channel:edit')")
    @Log(title = "商品货道关联", businessType = BusinessType.UPDATE)
    @PutMapping("/config")
    public AjaxResult editGoodsConfig(@RequestBody ChannelDto channelDto){
        //检验参数
        if(channelDto == null || channelDto.getInnerCode() == null || CollectionUtils.isEmpty(channelDto.getChannelList())){
            return AjaxResult.error("参数无效");
        }
        //获取货道信息进行批量修改
        List<Channel> channelList = channelDto.getChannelList();
        for (Channel channel : channelList) {
            if(channel!=null){
                channel.setUpdateTime(DateUtils.getNowDate());
                channelService.updateChannelList(channel);
            }
        }
        return AjaxResult.success();
    }
}
